package controllers

import (
	"github.com/revel/revel"
	"waterblabla/app/models"
)

type Postman struct {
	Controller
}

func (c Postman) Register() revel.Result {
	type Req struct {
		Qid   string `json:"qid"   valid:"Required"`
		Name        string `json:"name"   valid:"Required"`
		MobilePhone string `json:"mobilePhone"  valid:"Required;Mobile"`
	}
	var req Req
	if err := models.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderResult(err)
	}

	postman := models.Postman{Qid: req.Qid, Name: req.Name, MobilePhone: req.MobilePhone}
	return c.RenderResult(models.Register(&postman))
}

func (c Postman) Stats() revel.Result {
	type Req struct {
		models.QS `,inline valid:"Required"`
		From      int64 `bson:"from" json:"from"   valid:"Required"`
		To        int64 `bson:"to" json:"to"  valid:"Required"`
	}
	var req Req
	if err := models.ParseBodyAndValidate(c.body, &req); err != nil {
		return c.RenderResult(err)
	}

	count, err := models.Total(req.Qid, req.From, req.To)
	if err != nil {
		return c.RenderResult(0)
	}
	return c.RenderResult(count)
}
